const stageType} = require("./constants");


// 开始回合 准备阶段 摸牌阶段 计算收益 爆仓校验 建造阶段 结束阶段
const gameins = {
  limit4Res: 10,
  status: 0, //开始0 进行中1 结束2
  player: [], //
  desk: [],
  desk2: [],
  indexdesk: 0,
  indexplayer: 0,
  indexturn: 0,
  resources: [], //黄色 绿色 蓝色 白色 黑色
  resources2use: [],
  stage: 0, //开始阶段0 准备阶段1 摸牌阶段2 投资阶段3 计算收益4 爆仓校验5 爆仓阶段6 建造阶段7 结束阶段8 结算阶段9

}

function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1)); // 随机索引
        [array[i], array[j]] = [array[j], array[i]]; // 交换元素
  }
  return array;
}
const 开始游戏 = (game, players, ) => {
  game.status = 1
  game.players = shuffleArray(players.slice());
}

const xyghh下一个回合 = (game) => {
  game.indexturn++
  game.indexplayer++
  game.stage = stageType.kaishi
}

const 开始回合 = (index) => {
  game.stage = stageType.zhunbei
}

const 执行抽卡 = (game, player) => {
  抽卡(game, player, 2)
  game.stage = stageType.shengchan //计算收益
}
const 计算收益 = (game, player) => {
  for (let i = 0; i < game.resources2use.length; i++) {
    const item = game.resources2use[i]
    // 使用i2作为索引变量，从0开始，每次加1，直到小于10
    const arr = []
    for (let i2 = 0; i2 < item; i2++) {
      const item2 = item[i2]
      if (item2 === game.indexplayer) {
        if (game.resources[i] > 0) {
          game.resources[i]--
          player.resources.push(i)
          arr.push(item2)
        } else {

        }
      }
      else
      {
        arr.push(item2)
      }
    }
    game.resources2use[i] = arr
  }
}
const js结算 = async (game) => {
  game.stage = stageType.jiesuan
  xyghh下一个回合(game)
  
}
const jsjz结束建造 = (game,player) => {
  game.stage = stageType.jiesuan
  js结算(game)
}
const jianzao建造 = (game,player,card,resources)=> {
  player.buildings.push(card)
  player.hand.remove(card)
syzy使用资源(game,player,resources)  

  
}

const syzy使用资源 = (game,player,removeRes)=>{

for (var prop in removeRes) {
let index = player.resources.indexOf(prop);
if (index !== -1) {
  player.resources.splice(index, 1);
  game.resources[prop]++
}
  
}
    
} 
const tz投资 = function(game, player, indexRes) {
  player.amount--
  game.resources2use[indexRes].push(player.index)
};

const zxtz执行投资 = function(game, player, indexRes) {
  tz投资(game, player, indexRes)
};

const jstz结束投资 = async (game,player) => {
  game.stage = stageType.jianzao
} 

const bcjy爆仓校验 = function(game, player) {
  if (player.resources.length > game.limit4Res) {
    game.stage = stageType.baocangchuli //爆仓阶段
  }
};

const 爆仓处理 = (game, player, removeRes, newRes) => {
  for (var i = 0; i < removeRes.length; i++) {
    // Tab to edit
    game.resources[i]++
  }
  player.resources = newRes
  xyghh下一个回合(game)
}

const zytz转移投资 = function(game, player, fromIndex, toIndex) {
  game.resources2use[toIndex].push(player.index)
  const arr = []
  let flag = false
  for (let i = 0; i < game.resources2use[fromIndex]; i++) {
    const item = game.resources2use[fromIndex][i]
    if (item == player.index && flag == false) {
      flag = true
    } else {
      arr.push(item)
    }
  }
  game.resources2use[fromIndex] = arr
};


const 抽卡 = (game, player, count) => {
  while (count-- === 0) {
    player.shoupai.push(game.desk[game.indexdesk])
    game.desk[game.indexdesk] = null
    game.indexdesk++
    if (game.indexdesk == game.desk.length) {
      洗牌(game)
    }
  }
}
const 洗牌 = (game) => {
  const arr = shuffleArray(game.desk2)
  game.desk2 = []
  game.desk = arr
  game.indexdesk = 0
}